package generate

import com.lheia.jcb.Spider
import com.lheia.jcb.spider.UrlProcess
import com.lheia.tool.ToolClass
import com.lheia.tool.ToolFile
import com.lheia.tool.ToolString


class Generate {
static def main(def args){

//    def property = [:]
//    property.domainName = "Resources"
//    property.lowDomainName = ToolString.toLowerCaseFirstOne(property.domainName)
//    property.domainNameCN = "资源"
//    property.packageName = "com.shendudingzhi.wechat"
//    File temp = new File("."+File.separator+"temple"+ File.separator+"controller.tem")
//    String tempText =  temp.getText()
//    tempText = ToolString.replaceContent(tempText,property)
//    File newFile = new File("."+File.separator+"grails-app"+File.separator+"controllers"+File.separator+property.packageName.replace(".",File.separator),property.domainName+"Controller.groovy")
//    if(!newFile.parentFile.exists()){
//        newFile.parentFile.mkdirs()
//    }
//    newFile.setText(tempText,ToolString.UTF_8)
//    Class domainClass =Class.forName("com.shendudingzhi.wechat.Resources")
//    Table table = domainClass.getAnnotation(Table.class)
//    def property =[:]
//    property.domainName = domainClass.getSimpleName()
//    property.lowDomainName = ToolString.toLowerCaseFirstOne(property.domainName)
//    property.domainNameCN =  table.nameCN()
//    property.packageName = domainClass.getPackage().getName()
//    property.columnMap = [:]
//    domainClass.getDeclaredFields().each {
//        Column column = it.getAnnotation(Column.class)
//        if(column){
//            def dataMap = [:]
//            dataMap.tpye = it.getType()
//            dataMap.name = it.getName()
//            dataMap.nameCN = column.nameCN()
//            dataMap.isInTable = column.isInTable()
//            dataMap.isQuery = column.isQuery()
//            property.columnMap.put(dataMap.name,dataMap)
//        }
//    }
//    println(property)
    //加载模板
    Map generate = com.lheia.tool.ToolProperties.getGenerateProperties()
    def domainInfo = ToolClass.parseDomainClass(Spider.class)
    generate.temple.split(",").each {
        String templeName = it
        String templePath = generate.get(it+"Temp")
        File tempFolder = new File("./temple/"+templePath)
        List fileList = []
        if(tempFolder.directory){
            tempFolder.eachFile {
                fileList.add(it)
            }
        }else{
            fileList.add(tempFolder)
        }
        fileList.each {
            String tempText = it.getText()
            String suffix = generate.get(templeName+"Suffix")
            String dstFileName =( suffix.contains("groovy")?(File.separator+domainInfo.packageName.replace(".",File.separator)
                    +File.separator+domainInfo.domainName+ ToolString.toUpperCaseFirstOne(templeName)+suffix):
                    (File.separator+domainInfo.lowDomainName+File.separator+domainInfo.lowDomainName
                            +ToolString.toUpperCaseFirstOne(it.name.substring(0, it.name.lastIndexOf(".")))+suffix))
            String dstPath = (generate.get(templeName+"Path")+dstFileName)
            println("模板文件路径"+it)
            String dstText = ToolString.replaceTemp(tempText,domainInfo)
            ToolFile.writeFile(dstPath,dstText,ToolString.UTF_8)
        }
    }

//    String s = ".\\grails-app\\controllers"
//    File file = new File(s)
//    println file.parentFile.list()




}
}
